public class Test20 {
    public static void printMatrixClockWisely(int[][] numbers){
        if (numbers == null) return;
        int x = 0;
        int y = 0;
        //最后一圈起始的行号列号
        //（number-1）/2
        //一共（number-1）/2
        while(x*2 < numbers.length && y*2<numbers.length){
            printMatrixCircle(numbers,x,y);
            x++;
            y++;
        }
    }
    public static void printMatrixCircle(int[][] numbers,int x,int y){
        //数组行数
        int row = numbers.length;
        //数组列数
        int col = numbers.length;
        int i= x;
        int j = y;
        //输出上面的一行
        for(;j<col-y;j++) System.out.print(numbers[i][j]+" ");
        if (row-x-1>x){
            //输出右边的一行
            for(i = x+1;i<row-x;i++) System.out.print(numbers[i][j-1]+ " ");
        }
        if (col-y-1 >y && row-x-1>x){
            //输出下面的一行
            for(j=col-y-2;j>=y;j--) System.out.print(numbers[i-1][j] + " ");
        }
        if (row-x-2>x & col-y-1 >y){
            //输出左边一行
            for (i=row-x-2;i>x;i--) System.out.print(numbers[i][j+1] + " ");
        }
    }

    public static void main(String[] args) {
        int[][] numbers = {
                {1, 2, 3, 4, 5},
                {16, 17, 18, 19, 6},
                {15, 24, 25, 20, 7},
                {14, 23, 22, 21, 8},
                {13, 12, 11, 10, 9},
        };
        printMatrixClockWisely(numbers);
        System.out.println();
    }
}
